import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:getx_scaffold/getx_scaffold.dart';

/// 二维码扫描页面
class QRScannerView extends StatefulWidget {
  const QRScannerView({super.key});

  @override
  State<QRScannerView> createState() => _QRScannerViewState();
}

class _QRScannerViewState extends State<QRScannerView> {
  final MobileScannerController controller = MobileScannerController(
    detectionSpeed: DetectionSpeed.noDuplicates,
  );

  bool isScanned = false;

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  void _onDetect(BarcodeCapture capture) {
    if (isScanned) return;

    final List<Barcode> barcodes = capture.barcodes;
    for (final barcode in barcodes) {
      final String? code = barcode.rawValue;
      if (code != null && code.isNotEmpty) {
        isScanned = true;
        // 返回扫描结果
        Get.back(result: code);
        break;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios, color: Colors.white, size: 20.sp),
          onPressed: () => Get.back(),
        ),
        title: Text(
          '扫描群组二维码',
          style: TextStyle(
            fontSize: 18.sp,
            fontWeight: FontWeight.w600,
            color: Colors.white,
          ),
        ),
        centerTitle: true,
        actions: [
          IconButton(
            icon: Icon(Icons.flash_on, color: Colors.white, size: 24.sp),
            onPressed: () => controller.toggleTorch(),
          ),
        ],
      ),
      body: Stack(
        children: [
          // 扫描区域
          MobileScanner(
            controller: controller,
            onDetect: _onDetect,
          ),
          // 扫描框和提示
          _buildScanOverlay(),
        ],
      ),
    );
  }

  /// 扫描框和提示
  Widget _buildScanOverlay() {
    return Column(
      children: [
        Expanded(
          flex: 1,
          child: Container(
            color: Colors.black.withOpacity(0.5),
          ),
        ),
        // 扫描框区域
        Container(
          height: 250.h,
          margin: EdgeInsets.symmetric(horizontal: 50.w),
          child: Stack(
            children: [
              // 四个角的边框
              Positioned(
                top: 0,
                left: 0,
                child: Container(
                  width: 30.w,
                  height: 30.h,
                  decoration: BoxDecoration(
                    border: Border(
                      top: BorderSide(color: Colors.green, width: 3.w),
                      left: BorderSide(color: Colors.green, width: 3.w),
                    ),
                  ),
                ),
              ),
              Positioned(
                top: 0,
                right: 0,
                child: Container(
                  width: 30.w,
                  height: 30.h,
                  decoration: BoxDecoration(
                    border: Border(
                      top: BorderSide(color: Colors.green, width: 3.w),
                      right: BorderSide(color: Colors.green, width: 3.w),
                    ),
                  ),
                ),
              ),
              Positioned(
                bottom: 0,
                left: 0,
                child: Container(
                  width: 30.w,
                  height: 30.h,
                  decoration: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(color: Colors.green, width: 3.w),
                      left: BorderSide(color: Colors.green, width: 3.w),
                    ),
                  ),
                ),
              ),
              Positioned(
                bottom: 0,
                right: 0,
                child: Container(
                  width: 30.w,
                  height: 30.h,
                  decoration: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(color: Colors.green, width: 3.w),
                      right: BorderSide(color: Colors.green, width: 3.w),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
        Expanded(
          flex: 1,
          child: Container(
            color: Colors.black.withOpacity(0.5),
            child: Center(
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 40.w),
                child: Text(
                  '将二维码放入框内，即可自动扫描',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: Colors.white,
                  ),
                  textAlign: TextAlign.center,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
